//
//  MessageSettingVC.m
//  AILock
//
//  Created by 谭亚明 on 2020/7/9.
//  Copyright © 2020 tym. All rights reserved.
//

#import "MessageSettingVC.h"
#import "UserModel.h"
#import "UIButton+ImageTitleSpacing.h"
#import "BRDatePickerView.h"
#import "UserModel.h"
#import "FamilyModel.h"
#import "UserTool.h"
#import "FamilyModel.h"
#import "MessageSettingCell.h"
#import "Sizes.h"
#import "GroupShadowTableView.h"
#import "FamilyModel.h"
@interface MessageSettingVC ()<GroupShadowTableViewDelegate, GroupShadowTableViewDataSource>

@property(nonatomic,strong) UserMessageService *service;
@property(nonatomic,strong) MessageConfig *config;
@property (nonatomic, strong) GroupShadowTableView *tableView;
@property(nonatomic,weak) IBOutlet UISwitch *switBtn;
@property(nonatomic,weak) IBOutlet UISwitch *equiSetSwithBtn;

@property(nonatomic,weak) IBOutlet UIButton *timelineBtn;

@property(nonatomic,strong) IBOutlet UITableViewCell *firstCell;
@end

@implementation MessageSettingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackBtnFunc];
    [self addTitle:@"消息设置"];
    [self setTable];
    self.switBtn.onTintColor = rgba(235, 238, 241);
    self.switBtn.tintColor = rgba(235, 238, 241);
    self.equiSetSwithBtn.onTintColor = rgba(235, 238, 241);
    self.equiSetSwithBtn.tintColor = rgba(235, 238, 241);
    [self getInitMessageModel];
}

- (void)setTable {
    self.tableView = [[GroupShadowTableView alloc] initWithFrame:CGRectMake(0, 175, ScreenWidth, ScreenHeight-NavigateonHeight-175-SafeBottomMargin) style:UITableViewStyleGrouped];
    self.tableView.headTitles = @[@"接收设备通知"];
    self.tableView.isShowArray = @[@(1)];
    self.tableView.groupShadowDelegate = self;
    self.tableView.groupShadowDataSource = self;
    self.tableView.backgroundColor = viewBGColor;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0.01)];
    self.tableView.sectionFooterHeight = 0;
    _tableView.showSeparator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
}

-(void)setTopViewData{
    [self.switBtn setOn:self.config.userMessageIsOpen];
    if (self.config.userEquipmentMessageTotalConfigData) {
        [self.equiSetSwithBtn setOn:self.config.userEquipmentMessageTotalConfigData.open];
    }else{
        [self.equiSetSwithBtn setOn:YES];
    }
    [self chaneSwithchBtnUI];
    
    if (!self.config.userMessageIsOpen) {
        self.switBtn.thumbTintColor = rgba(155, 155, 155);
    }else{
        self.switBtn.thumbTintColor = rgba(76, 132, 255);
    }
    [self.timelineBtn setTitle:[NSString stringWithFormat:@"%@-%@",self.config.startTime?self.config.startTime:@"",self.config.endTime?self.config.endTime:@""] forState:UIControlStateNormal];
    [self.timelineBtn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:2];
}

-(UserMessageService*)service{
    if (_service == nil) {
        _service = [[UserMessageService alloc] init];
    }
    return _service;
}

-(void)getInitMessageModel{
    TYMWeak(self);
    [self.service getFindusermessageconfig:[UserTool getCurrentFamily].familyId completion:^(LMJBaseResponse *respon, MessageConfig *config) {
        weakself.config = config;
        [weakself setTopViewData];
        [weakself.tableView reloadData];
    }];
}

-(void)chaneSwithchBtnUI{
    if (!self.switBtn.on) {
        self.switBtn.thumbTintColor = rgba(155, 155, 155);
        [self.timelineBtn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    }else{
        self.switBtn.thumbTintColor = rgba(76, 132, 255);
        [self.timelineBtn setImage:[UIImage imageNamed:@"icon_list_more"] forState:UIControlStateNormal];
    }
    
    if (!self.equiSetSwithBtn.on) {
        self.equiSetSwithBtn.thumbTintColor = rgba(155, 155, 155);
    }else{
        self.equiSetSwithBtn.thumbTintColor = rgba(76, 132, 255);
    }
    [self.timelineBtn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:2];
}

-(IBAction)changSwitchAction:(UISwitch*)sender{
    [self chaneSwithchBtnUI];
    [self.service getUserMessageconfigId:self.config.userMessageConfigId startTime:self.config.startTime endTime:self.config.endTime familyId:[UserTool  getCurrentFamily].familyId isOpen:self.switBtn.on completion:^(LMJBaseResponse *respon) {
        
    }];
}

-(IBAction)changEquipmentMessage:(id)sender{
    if (!self.equiSetSwithBtn.on) {
        self.equiSetSwithBtn.thumbTintColor = rgba(155, 155, 155);
    }else{
        self.equiSetSwithBtn.thumbTintColor = rgba(76, 132, 255);
    }
    
    TYMWeak(self);
    [self.service getEquipmentmessageconfigId:self.config.userEquipmentMessageTotalConfigData.equipmentMessageConfigId type:1 familyId:[UserTool  getCurrentFamily].familyId  isOpen:self.equiSetSwithBtn.on equipmentId:0 completion:^(LMJBaseResponse *respon) {
        
        if (!respon.message) {
            weakself.config.userEquipmentMessageTotalConfigData.open = weakself.equiSetSwithBtn.on;
            
            
            [weakself.tableView reloadData];
        }else{
            [WHToast showMessage:respon.message duration:2 finishHandler:nil];
        }
    }];
}

-(IBAction)changeTimelineAction:(UIButton*)btn{
    
    if (!self.switBtn.on) {
        return;
    }
    BRDatePickerView *datePickerView = [[BRDatePickerView alloc]init];
    datePickerView.pickerMode = BRDatePickerModeHM;
    datePickerView.title = @"请选择开始时间";
    NSDate *data = [NSDate br_getDate:self.config.startTime format:@"HH:mm"];
    datePickerView.selectDate = [NSDate br_setHour:data.br_hour minute:data.br_minute];
    datePickerView.isAutoSelect = NO;
    TYMWeak(self);
    datePickerView.resultBlock = ^(NSDate *selectDate, NSString *selectValue) {
        weakself.config.startTime = selectValue;
        [weakself setEndTime];
    };
    BRPickerStyle *customStyle = [[BRPickerStyle alloc]init];
    customStyle.topCornerRadius = 10;
    customStyle.pickerTextColor = rgba(51, 51, 51);
    customStyle.separatorColor = rgba(234, 234, 234);
    customStyle.cancelTextColor = rgba(102, 102, 102);
    customStyle.doneTextColor = rgba(51, 51, 51);
    customStyle.hiddenTitleLine = YES;
    customStyle.rowHeight = 42;
    customStyle.pickerHeight = 225;
    customStyle.cancelTextFont = Font_Regular(15);
    customStyle.doneTextFont  = Font_Regular(15);
    customStyle.pickerTextFont = Font_Regular(22);
    datePickerView.pickerStyle = customStyle;
    [datePickerView show];
}

-(void)setEndTime{
    
    BRDatePickerView *datePickerView = [[BRDatePickerView alloc]init];
    datePickerView.pickerMode = BRDatePickerModeHM;
    datePickerView.title = @"请选择结束时间";
    NSDate *data = [NSDate br_getDate:self.config.endTime format:@"HH:mm"];
    datePickerView.selectDate = [NSDate br_setHour:data.br_hour minute:data.br_minute];
    datePickerView.isAutoSelect = NO;
    TYMWeak(self);
    datePickerView.resultBlock = ^(NSDate *selectDate, NSString *selectValue) {
        weakself.config.endTime = selectValue;
        [weakself.timelineBtn setTitle:[NSString stringWithFormat:@"%@-%@",self.config.startTime,weakself.config.endTime] forState:UIControlStateNormal];
        [weakself changSwitchAction:weakself.switBtn];
    };
    BRPickerStyle *customStyle = [[BRPickerStyle alloc]init];
    customStyle.topCornerRadius = 10;
    customStyle.pickerTextColor = rgba(51, 51, 51);
    customStyle.separatorColor = rgba(234, 234, 234);
    customStyle.cancelTextColor = rgba(102, 102, 102);
    customStyle.doneTextColor = rgba(51, 51, 51);
    customStyle.hiddenTitleLine = YES;
    customStyle.rowHeight = 42;
    customStyle.pickerHeight = 225;
    customStyle.cancelTextFont = Font_Regular(15);
    customStyle.doneTextFont  = Font_Regular(15);
    customStyle.pickerTextFont = Font_Regular(22);
    datePickerView.pickerStyle = customStyle;
    [datePickerView show];
}

#pragma mark delegate datasource
- (NSInteger)numberOfSectionsInGroupShadowTableView:(GroupShadowTableView *)tableView {
    return 1;
}

- (NSInteger)groupShadowTableView:(GroupShadowTableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.config.userEquipmentMessageConfigList.count + 1;
}

- (CGFloat)groupShadowTableView:(GroupShadowTableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [MessageSettingCell cellForHeight];
}

- (UITableViewCell *)groupShadowTableView:(GroupShadowTableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row==0) {
        return self.firstCell;
    }
    
    MessageSettingCell  *cell = [tableView dequeueReusableCellWithIdentifier:[MessageSettingCell identifier]];
    if (!cell) {
        cell = [[MessageSettingCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:[MessageSettingCell identifier]];
    }
    [cell setUserModel:self.config.userEquipmentMessageConfigList[indexPath.row-1]];
    
    if (self.config.userEquipmentMessageTotalConfigData) {
        if (!self.config.userEquipmentMessageTotalConfigData.open) {
            [cell setChangeMessageBtn:NO];
        }else{
            [cell setChangeMessageBtn:YES];
        }
    }else{
        [cell setChangeMessageBtn:YES];
    }
    TYMWeak(self);
    cell.equmentMessageSettingBlock = ^(BOOL isOpen) {
        [weakself setEqumentMessageWithOpen:isOpen withIndex:indexPath.row-1];
    };
    return cell;
}

-(void)setEqumentMessageWithOpen:(BOOL)isOpen withIndex:(NSInteger)index
{
    UserMessageModel *messageModel = self.config.userEquipmentMessageConfigList[index];
    [self.service getEquipmentmessageconfigId:messageModel.equipmentMessageConfigId type:2 familyId:[UserTool getCurrentFamily].familyId isOpen:isOpen equipmentId:messageModel.equipmentId completion:^(LMJBaseResponse *respon) {
        if (respon.code == 0) {
            messageModel.open = isOpen;
        }
    }];
}

-(void)groupShadowTableView:(GroupShadowTableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}

@end
